	<template>
	<view class="conter">


		<view class="header" style="color: #000000;" @tap="goBack(id)">
			<view class="arrow">
			</view>
			<view class="title">
				{{title}}
			</view>
		</view>
		
		
		<!-- <view class="text-tab">
			<view class="box">
				<view class="marige-bor" v-for="(item, index) in textList" :key="index">
					<view class="diso-flex">
						<view class="">
							{{item.name}}
						</view>
						<view class="conter-image" @tap="skip(item)">
							<u-radio-group v-model="value">
								<u-radio active-color="#AFB3F1" :name="item.id" ></u-radio>
							</u-radio-group>
						</view>
					</view>
				</view>
			</view>
		</view> -->


		<view class="padding-left padding-right padding-top margin-bottom">
			<u-search action-text="搜索"
				@change="getGoodsListFrom1"
				:show-action="true"
				@clear="clearSearch"
				placeholder="搜索类名..." v-model="keyword"
				:animation="true">
			</u-search>
		</view>







	

		<u-tabs :list="listFrom" bg-color="#fff" :current="current" active-color="#7578EF" :is-scroll="false"
			@change="change">
		</u-tabs>

		
		<view v-if="current == 0" v-for="(item, index) in textListFrom" :key="index"
			class="flex align-center margin-top u-border-bottom wh96"
			style="padding: 20rpx; margin: 10rpx auto;
			background-color: #FFFFFF;"
			>
			 <!-- @tap="goBackAdd1(item)" 这里是修改商品 -->
			<image  class="w150 h150" :src="img + item.image" mode=""></image>
			<view class="padding-left" style="line-height: 60rpx;">
				<view>{{item.name}}</view>
				<view>单位：{{item.unit_name}}</view>
			</view>
		</view>
		<view  v-if="current == 1" class="text-tab">
			<view class="box">
				<view class="marige-bor" v-for="(item, index) in textListFromType" :key="index">
					<view class="diso-flex" @tap="getGoodsListFrom2(item.id)">
						<view class="">
							{{item.type_name}}
						</view>
						<view class="conter-image" @tap="getGoodsListFrom2(item.id)">
						</view>
					</view>
				</view>
			</view>
		</view>


		<!-- popupShow = true -->
		
		<!-- <view class="add" @tap="goBackAdd('/pages/shopping/addShopping')">
			<image class="w100 h100" src="../../static/index/add.png" mode=""></image>
		</view> -->




		<!-- 底部弹层 -->
		<u-popup v-model="redactShow" mode="bottom" width="100%" height="426rpx" border-radius="40">
			<view class="padd-text" v-for="(item, index) in list" :key="index" @click="elect(item, index)">
				<text :class="[index == tabText ? 'color-text' : 'none-color']">{{item.name}}</text>
			</view>
		</u-popup>




		<u-popup v-model="popupShow" mode="center" width="70%" height="280rpx" border-radius="10">
			<view class="input-text">
				添加商品分类
			</view>
			<view class="input-val">
				<input type="text" v-model="name" style="background-color: #F0F0F0; text-align: center;height: 100%;"
					placeholder="请输入商品类目" />
			</view>
			<view class="text-select">
				<view class="" @click="popupShow = false">
					取消
				</view>
				<view class="" @click="addGoods">
					确定
				</view>
			</view>
		</u-popup>


		<u-popup v-model="popupShow1" mode="center" width="70%" height="280rpx" border-radius="10">
			<view class="input-text">
				修改-{{removeName}}
			</view>
			<view class="input-val">
				<input type="text" v-model="name" style="background-color: #F0F0F0; text-align: center;height: 100%;"
					placeholder="请输入修改名称" />
			</view>
			<view class="text-select">
				<view class="" @click="popupShow1 = false">
					取消
				</view>
				<view class="" @click="removeClass">
					确定
				</view>
			</view>
		</u-popup>

		<u-popup v-model="popupShow2" mode="center" width="70%" height="200rpx" border-radius="10">
			<view class="input-text">
				删除-{{removeName}}
			</view>
			<!-- <view class="input-val">
				<input type="text" v-model="name" style="background-color: #F0F0F0; text-align: center;height: 100%;" placeholder="请输入修改名称" />
			</view> -->
			<view class="text-select">
				<view class="" @click="popupShow2 = false">
					取消
				</view>
				<view class="" @click="deleteClass">
					确定
				</view>
			</view>
		</u-popup>

		<u-popup v-model="popupShow3" mode="bottom" border-radius="10">

			<view class="wrap">
				<u-waterfall v-model="textListFromType" ref="uWaterfall">
					<template v-slot:left="{leftList}"> 
					<!-- textListFrom type -->
						<view class="demo-warter" v-for="(item, index) in leftList" :key="index" @tap="getGoodsListFrom2(item.id)">
							<view class="demo-shop">
								{{item.name}}
							</view>
						</view>
					</template>
					<template v-slot:right="{rightList}">
						<view class="demo-warter" v-for="(item, index) in rightList" :key="index" @tap="getGoodsListFrom2(item.id)">
							<view class="demo-shop">
								{{item.name}}
							</view>
						</view>
					</template>
				</u-waterfall>
				<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
			</view>

		</u-popup>
	</view>
</template>

<script>
	import headerBox from '../../components/header/header.vue'

	export default {
		components: {
			headerBox
		},
		data() {
			return {
				title: '分类',
				textList: [],
				addUrl: '',
				redactShow: false,
				list: [{
						name: '编辑',
						show: true
					}, {
						name: '查看',
						show: true
					},
					// {
					// 	name: '删除',
					// 	show: true
					// },
					{
						name: '取消',
						show: false
					}
				],
				listFrom: [{
					name: '全部'
				}, {
					name: '筛选'
				}],
				current: 0,
				tabText: 0,
				popupShow: false,
				popupShow1: false,
				popupShow2: false,
				popupShow3: false,
				value: '',
				name: '',
				removeName: '',
				id: '',
				num: '',
				loadStatus: 'loadmore',
				flowList: [],
				page: 1,
				pagesize: 10,
				textListFrom: [],
				textListFromType: [],
				keyword: '',
				img: this.$images,
				type_text: '',
			}
		},
		onLoad() {
			// this.getGoodsList()
			this.getGoodsListFrom()
		},
		onShow() {
			this.redactShow = false
			// this.addRandomData();
		},
		onReachBottom() {
			this.page++
			this.getGoodsListFrom()
		},
		methods: {
			goBackAdd(url) {
				uni.navigateTo({
					url,
				})
			},
			
			goBackAdd1(item) {
				console.log(item)
				// uni.navigateTo({
				// 	url: '/pages/shopping/alterPhone?item=' + JSON.stringify(item)
				// })
				uni.setStorageSync('item', item)
				uni.navigateTo({
					url: '/pages/shopping/alterPhone'
				})
			},
			
			
			addRandomData() {
				for (let i = 0; i < 10; i++) {
					let index = this.$u.random(0, this.list.length - 1);
					// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
					let item = JSON.parse(JSON.stringify(this.list[index]))
					item.id = this.$u.guid();
					this.flowList.push(item);
				}
			},

			change(index) {
				console.log(index)
				// if (index == 1 ) {
				// 	this.type_text = 1
					
				// 	this.current = index;
				// 	this.textListFrom = []
				// 	this.textListFromType = []
				// 	this.page = 1
				// 	this.getGoodsListFrom()
				// } else {
					// this.type_text = ''
					
					this.current = index;
					this.textListFrom = []
					this.textListFromType = []
					this.page = 1
				// }
					this.getGoodsListFrom()
				
			},

			// 得到商品列表
			async getGoodsList() {
				const {
					data: res
				} = await this.$u.post("api/jinxiao.Classify/index");
				this.value = res[0].id
				this.className = res[0].name
				this.textList = res
				console.log('得到商品列表', res)
			},
			
			async getGoodsListFrom(id) {
				const res = await this.$u.post("api/Invoic/goods_find",{
					page: this.page,
					pagesize: this.pagesize,
					// type_id: this.type_text ? type_text : '',
					search: this.keyword ? this.keyword : '',
					type_id: id ? id : '',
				});
				this.popupShow3 = false
				this.textListFrom = [...this.textListFrom,...res.data.goods_list]
				this.textListFromType = [...this.textListFromType,...res.data.goods_type]
				// this.textListFrom = res.data
			},
			
			clearSearch() {
				this.getGoodsListFrom()
			},
			
			 getGoodsListFrom1(id) {
				this.textListFrom = []
				this.getGoodsListFrom()
				// const res = await this.$u.post("/api/jinxiao.Goods/goods_search",{
				// 	page: this.page,
				// 	pagesize: this.pagesize,
				// 	search: this.keyword ? this.keyword : '',
				// });
				// this.popupShow3 = false
				// this.textListFrom = [...this.textListFrom,...res.data.$lists]
			},
			
			async getGoodsListFrom2(id) {
				console.log('1111111111')
				
				this.textListFrom = []
				this.textListFromType = []
				this.current = 0
				this.getGoodsListFrom(id)
				
				// this.current = 0
				// this.textListFrom = []
				// console.log('sosuo111',this.keyword,id == '',id)
				// const res = await this.$u.post("/api/jinxiao.Goods/goods_search",{
				// 	page: this.page,
				// 	pagesize: this.pagesize,
				// 	classify_id: id ? id : '',
				// });
				// this.popupShow3 = false
				// this.textListFrom = [...this.textListFrom,...res.data.$lists]
				// this.textListFromType = res.data.type
				// // this.textListFrom = res.data
			},


			// 添加商品
			async addGoods() {
				if (this.name == '') return this.$tools.msg('请输入商品类目')
				const res = await this.$u.post("api/jinxiao.Classify/add?name=" + this.name);
				if (res.code !== 1) return this.$tools.msg(res.msg)
				this.popupShow = false
				this.getGoodsList()
				this.$tools.msg(res.msg)
				console.log(res)
			},


			// 确定修改
			async removeClass() {
				const res = await this.$u.post("api/jinxiao.Classify/edit?name=" + this.name + "&id=" + this.id);
				if (res.code !== 1) return this.$tools.msg(res.msg)
				this.$tools.msg(res.msg)
				this.getGoodsList()
				this.popupShow1 = false
				this.redactShow = false
				this.name = ''
				console.log('确定修改', res)
			},





			goBack(id) {
				console.log(id)
				uni.navigateBack({
					delta: 1
				})
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				prevPage.$vm.classId = this.value; //修改上一页data里面的searchVal参数值为1211
				prevPage.$vm.className = this.className;

			},


			skip(item) {
				this.redactShow = true
				this.removeName = item.name
				this.id = item.id
				console.log('this.id ', this.id)

			},

			// 删除类目
			async deleteClass() {
				if (this.id == '') return this.$tools.msg('请选择类目')
				const res = await this.$u.post("api/jinxiao.Classify/delete?id=" + this.id);
				if (res.code !== 1) return this.$tools.msg(res.msg)
				this.$tools.msg(res.msg)
				this.getGoodsList()
				this.popupShow2 = false
				this.redactShow = false
				console.log('确定修改', res)
			},

			elect(item, index) {
				console.log(item)
				this.tabText = index
				this.redactShow = item.show
				if (index == 0) {
					console.log('000000', this.id)
					if (this.id == '') return this.$tools.msg('请选择')
					this.popupShow1 = true
					this.name = ''
				} else if (index == 1) {

					uni.navigateTo({
						url: '/pages/shopping/index?classId=' + this.id + '&name=' + this.removeName
					})
					// this.getGoodsList()
					// 调用删除事件
				} else if (index == 2) {
					this.popupShow2 = true
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.conter {
		width: 100%;
		position: relative;
		
		.demo-warter {
				border-radius: 8px;
				margin: 5px;
				background-color: #ffffff;
				padding: 8px;
				position: relative;
			}
			
			.u-close {
				position: absolute;
				top: 32rpx;
				right: 32rpx;
			}
			
			.demo-image {
				width: 100%;
				border-radius: 4px;
			}
			
			.demo-title {
				font-size: 30rpx;
				margin-top: 5px;
				color: $u-main-color;
			}
			
			.demo-tag {
				display: flex;
				margin-top: 5px;
			}
			
			.demo-tag-owner {
				background-color: $u-type-error;
				color: #FFFFFF;
				display: flex;
				align-items: center;
				padding: 4rpx 14rpx;
				border-radius: 50rpx;
				font-size: 20rpx;
				line-height: 1;
			}
			
			.demo-tag-text {
				border: 1px solid $u-type-primary;
				color: $u-type-primary;
				margin-left: 10px;
				border-radius: 50rpx;
				line-height: 1;
				padding: 4rpx 14rpx;
				display: flex;
				align-items: center;
				border-radius: 50rpx;
				font-size: 20rpx;
			}
			
			.demo-price {
				font-size: 30rpx;
				color: $u-type-error;
				margin-top: 5px;
			}
			
			.demo-shop {
				font-size: 22rpx;
				color: $u-tips-color;
				margin-top: 5px;
				text-align: center;
			}
		
		
		
		

		.header {
			width: 100%;
			height: 150rpx;
			line-height: 150rpx;
			display: flex;
			align-items: center;
			// padding-left: 10rpx;
			padding-top: 30rpx;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			background: linear-gradient(to right, #656CE6, #9397FC);

			.arrow {
				position: relative;
				display: inline-block;
			}

			.arrow::before {
				content: '';
				width: 10px;
				height: 10px;
				border: 0px;
				border-bottom: solid 2px #fff;
				border-left: solid 2px #fff;
				-ms-transform: rotate(45deg);
				-webkit-transform: rotate(45deg);
				transform: rotate(45deg);
				position: absolute;
				top: 50%;
				left: 0;
				margin-top: -4px;
				margin-left: 28rpx;
			}

			.title {
				color: #fff;
				margin: 0 auto;
			}
		}


		.text-tab {
			width: 100%;
			// padding: 30rpx;
			padding-bottom: 0;


			.box {
				width: 98%;
				margin: 0 auto;
				background-color: #FFFFFF;
				border-radius: 10rpx;
			}

			.marige-bor {
				width: 90%;
				margin: 0 auto;
				color: #333333;
				display: flex;
				font-size: 28rpx;
				align-items: center;
				border-bottom: 1rpx solid #F0F0F0;
				justify-content: space-between;

				.diso-flex {
					width: 100%;
					height: 80rpx;
					display: flex;
					align-items: center;
					background-color: #FFFFFF;
					justify-content: space-between;
				}

				.conter-image {
					width: 22rpx;
					height: 36rpx;
					font-weight: 700;
					color: #999;
				}
			}

			.marige-bor:last-child {
				border-bottom: 0;
			}
		}

		// 加号
		.add {
			position: fixed;
			bottom: 200rpx;
			right: 40rpx;
			width: 100rpx;
			height: 100rpx;
			text-align: center;
			line-height: 100rpx;
			border-radius: 50%;
			font-size: 100rpx;
			color: #FFFFFF;
		}

		.total {
			width: 100%;
			position: absolute;
			bottom: 0;
			left: 0;
			height: 100rpx;
			padding: 0 60rpx 0 30rpx;
			background-color: #FFFFFF;
		}

	}

	.padd-text {
		width: 80%;
		height: 106rpx;
		line-height: 106rpx;
		text-align: center;
		font-size: 28rpx;
		color: #333;
		border-bottom: 1rpx solid #EEEEEE;
		margin: 0 auto;
	}

	.color-text {
		color: #7278EC;
	}

	.none-color {
		color: #000;
	}

	.input-text {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		margin-top: 10rpx;
	}

	.input-val {
		width: 80%;
		height: 60rpx;
		line-height: 60rpx;
		margin: 0 auto 30rpx;
	}

	.text-select {
		width: 90%;
		margin: 0 auto;
		border-top: 1rpx solid #F0F0F0;
		display: flex;
		color: #AFB3F1;
		// padding-top: 20rpx;
		justify-content: space-around;

		view {
			width: 50%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			border-right: 1rpx solid #F0F0F0;
		}

		view:last-child {
			border-right: 0;
		}
	}
</style>
